var currentName = '未定义';
$(function(){
	initInstructorEdits($("#instructor_table"));
	initRecordEdits($("#table1"));
	initRecordEdits($("#table2"));
	initRecordEdits($("#table3"));
	initRecordEdits($("#table4"));
	initRecordEdits($("#table5"));
});
function change(src){
	$.post('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		id: 'i.avatar_link',
		value: src
	});
	$("#avatar_img").attr("src", "/pics/storage/"+src+".jpg");
}
function initInstructorEdits($context){
	$(".edit_text", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑'
	});
	$(".edit_area", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        type: 'textarea'
	});
	$(".date_picker", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        type: 'datepicker'
	});
	$(".select_gender", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	return {name: currentName};
        },
        type: 'select',
        data: " {'0':'未知','1':'男','2':'女'}"
	});
	$(".select_nation", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	return {name: currentName};
        },
        type: 'select',
        loadurl: '/data/listNations.action'
	});
	$(".select_jg", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	return {name: currentName};
        },
        type: 'select',
        loadurl: '/data/listProvinces.action'
	});
	$(".select_zzmm", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	return {name: currentName};
        },
        type: 'select',
        loadurl: '/data/listZzmms.action'
	});
	$(".select_marridge", $context).editable('/homeAdmin/updateInstructor.action?instructorId='+$("#instructor_id").val(),{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	return {name: currentName};
        },
        type: 'select',
        data: " {'0':'未婚','1':'已婚'}"
	});
	$('select,input', $context).live('change', function () {
		currentName = $("option:selected", $(this)).text();
        $(this).parent().submit();
    });
}
function initRecordEdits($context){
	$(".addRecord", $context).click(function(){
		$table = $(this).parent().parent().parent().parent();
		$.post("/homeAdmin/addRecord.action", {
			instructorId: $("#instructor_id").val(),
			type: $table.attr("id").substring(5,6)
			}, function(data){
			$e = $(".copy", $table).clone(false);
			$e.removeClass("copy");
			$e.attr("name", data);
			$e.appendTo($table);
			initRecordEdits($e);
		});
	});
	$(".delete_record", $context).click(function(){
		$row = $(this).parent().parent();
		$.post("/homeAdmin/deleteRecord.action",{
			recordId: $row.attr("name"),
			type: $row.parent().parent().attr("id").substring(5,6)
		}, function(data){
			if(data=='success'){
				$row.remove();
			}else{
				alert(data);
			}
		});
	});
	$(".record_text", $context).editable('/homeAdmin/updateRecord.action',{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	$row = $(this).parent();
        	return {
        		id: 'x.'+$(this).attr("name"),
            	recordId: $row.attr("name"),
            	type: $row.parent().parent().attr("id").substring(5,6)
        	};
        }
	});
	$(".record_date_picker", $context).editable('/homeAdmin/updateRecord.action',{
		indicator : '保存中',
        tooltip   : '单击编辑',
        type: 'datepicker',
        submitdata: function(){
        	$row = $(this).parent();
        	return {
        		id: 'x.'+$(this).attr("name"),
            	recordId: $row.attr("name"),
            	type: $row.parent().parent().attr("id").substring(5,6)
        	};
        }
	});
	$(".select_independent", $context).editable('/homeAdmin/updateRecord.action',{
		indicator : '保存中',
        tooltip   : '单击编辑',
        submitdata: function(){
        	$row = $(this).parent();
        	return {
        		id: 'x.'+$(this).attr("name"),
        		name: currentName,
            	recordId: $row.attr("name"),
            	type: $row.parent().parent().attr("id").substring(5,6)
        	};
        },
        type: 'select',
        data: " {'0':'合作','1':'独立'}"
	});
	$('select', $context).live('change', function () {
		currentName = $("option:selected", $(this)).text();
        $(this).parent().submit();
    });
}